package com.wb.common;

import com.wb.util.StringUtil;
import com.wb.util.WebUtil;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.json.JSONArray;
import org.json.JSONObject;

public class UserList {
	private static HashMap<String, HashSet<HttpSession>> users = new HashMap();

	public static void add(String userId, HttpSession session) {
		if (Var.recordSession) {
			doAdd(userId, session);
		}

	}

	private static synchronized void doAdd(String userId, HttpSession session) {
		HashSet<HttpSession> sessions = (HashSet) users.get(userId);
		if (sessions == null) {
			sessions = new HashSet();
			users.put(userId, sessions);
		}

		sessions.add(session);
	}

	public static void remove(String userId, HttpSession session) {
		if (Var.recordSession) {
			doRemove(userId, session);
		}

	}

	private static synchronized void doRemove(String userId, HttpSession session) {
		HashSet<HttpSession> sessions = (HashSet) users.get(userId);
		if (sessions != null && session != null) {
			sessions.remove(session);
			if (sessions.isEmpty()) {
				users.remove(userId);
			}
		}

	}

	public static void invalidate(String[] userIds) {
		if (Var.recordSession) {
			doInvalidate(userIds);
		}

	}

	private static synchronized void doInvalidate(String[] userIds) {
		String[] var6 = userIds;
		int var5 = userIds.length;

		for (int var4 = 0; var4 < var5; ++var4) {
			String userId = var6[var4];
			HashSet<HttpSession> sessions = (HashSet) users.get(userId);
			if (sessions != null) {
				HttpSession[] sessionArray = (HttpSession[]) sessions.toArray(new HttpSession[sessions.size()]);
				HttpSession[] var10 = sessionArray;
				int var9 = sessionArray.length;

				for (int var8 = 0; var8 < var9; ++var8) {
					HttpSession session = var10[var8];

					try {
						session.invalidate();
					} catch (Throwable var12) {
						;
					}
				}

				users.remove(userId);
			}
		}

	}

	public static void update(String userId, String[] roles, boolean status) {
		if (Var.recordSession) {
			doUpdate(userId, roles, status);
		}

	}

	private static synchronized void doUpdate(String userId, String[] roles, boolean status) {
		HashSet<HttpSession> sessions = (HashSet) users.get(userId);
		if (sessions != null) {
			if (status) {
				Iterator var5 = sessions.iterator();

				while (var5.hasNext()) {
					HttpSession session = (HttpSession) var5.next();

					try {
						session.setAttribute("sys.roles", roles);
						session.setAttribute("sys.roleList", StringUtil.join(roles, ","));
					} catch (Throwable var7) {
						;
					}
				}
			} else {
				String[] userIds = new String[]{userId};
				invalidate(userIds);
			}
		}

	}

	public static synchronized HttpSession[] getSessions(String userId) {
		HashSet<HttpSession> sessions = (HashSet) users.get(userId);
		return sessions == null ? null : (HttpSession[]) sessions.toArray(new HttpSession[sessions.size()]);
	}

	public static synchronized String[] getUsers() {
		return (String[]) users.keySet().toArray(new String[users.size()]);
	}

	public static void getSessionList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (Var.recordSession) {
			doGetSessionList(request, response);
		}

	}

	private static synchronized void doGetSessionList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		String user = request.getParameter("user");
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		int index = -1;
		JSONArray rows = new JSONArray();
		HashSet<HttpSession> sessions = (HashSet) users.get(user);
		if (limit > Var.limitRecords) {
			limit = Var.limitRecords;
		}

		int end = start + limit;
		if (sessions != null) {
			Iterator var12 = sessions.iterator();

			while (var12.hasNext()) {
				HttpSession session = (HttpSession) var12.next();
				++index;
				if (index >= start) {
					if (index >= end) {
						break;
					}

					JSONObject row = new JSONObject();

					try {
						row.put("ip", session.getAttribute("sys.ip"));
						row.put("userAgent", session.getAttribute("sysx.userAgent"));
						row.put("createDate", new Date(session.getCreationTime()));
						row.put("lastAccessDate", new Date(session.getLastAccessedTime()));
						rows.put(row);
					} catch (Throwable var14) {
						--index;
					}
				}
			}
		}

		JSONObject result = new JSONObject();
		result.put("rows", rows);
		result.put("total", sessions == null ? 0 : sessions.size());
		WebUtil.send(response, result);
	}

	public static void getUserList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (Var.recordSession) {
			doGetUserList(request, response);
		}

	}

	private static synchronized void doGetUserList(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		int index = -1;
		JSONArray rows = new JSONArray();
		Set<Entry<String, HashSet<HttpSession>>> es = users.entrySet();
		if (limit > Var.limitRecords) {
			limit = Var.limitRecords;
		}

		int end = start + limit;
		Iterator var14 = es.iterator();

		while (var14.hasNext()) {
			Entry<String, HashSet<HttpSession>> e = (Entry) var14.next();
			++index;
			if (index >= start) {
				if (index >= end) {
					break;
				}

				HashSet<HttpSession> sessions = (HashSet) e.getValue();
				int sessionCount = 0;
				HttpSession session = null;

				for (Iterator var16 = sessions.iterator(); var16.hasNext(); ++sessionCount) {
					HttpSession sess = (HttpSession) var16.next();
					if (session == null) {
						session = sess;
					}
				}

				if (sessionCount != 0) {
					JSONObject row = new JSONObject();
					row.put("sessionCount", sessionCount);
					row.put("user", e.getKey());

					try {
						row.put("username", session.getAttribute("sys.username"));
						row.put("dispname", session.getAttribute("sys.dispname"));
						row.put("ip", session.getAttribute("sys.ip"));
						row.put("userAgent", session.getAttribute("sysx.userAgent"));
						rows.put(row);
					} catch (Throwable var17) {
						--index;
					}
				}
			}
		}

		JSONObject result = new JSONObject();
		result.put("rows", rows);
		result.put("total", users.size());
		WebUtil.send(response, result);
	}
}